package com.xiaomaoguai.httpclient;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

/**
 * @Description :
 * @FileName: Http1.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年5月12日 下午4:57:52
 * @Version:V1.00
 */
public class Http1 {

	private final static String IMG_URL = "http://gi2.md.alicdn.com/bao/uploaded/i2/TB12s3gHpXXXXXBXFXXXXXXXXXX_!!0-item_pic.jpg_430x430q90.jpg";

	public static void main(String[] args) throws Exception {
		HttpClientBuilder builder = HttpClientBuilder.create();

		CloseableHttpClient closeableHttpClient = builder.build();

		HttpGet get = new HttpGet(IMG_URL);

		CloseableHttpResponse httpResponse = closeableHttpClient.execute(get);

		HttpEntity entity = httpResponse.getEntity();

		InputStream inputStream = entity.getContent();

		BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

		FileOutputStream fileOutputStream = new FileOutputStream(new File("C://tst.jpg"));

		int read;
		while ((read = bufferedInputStream.read()) != -1 ) {
			fileOutputStream.write(read);
		}
		fileOutputStream.close();
		bufferedInputStream.close();
		inputStream.close();
	}
}
